import router from "@/router"
import { useBreadCrumb } from "../store/breadCrumb"
import * as api from "../api/permission"
import { usePermission } from "../store/permission"
import NProgress from "nprogress"
router.beforeEach(async (to, from) => {
    // 打开进度条
    NProgress.start()
     let permissStore = usePermission()


    // 获取token
    let token = sessionStorage.getItem('zjl-token')
    // 如果没有token
    if (!token) {
        // 如果地址不是login
        if (to.path != '/login') {
            return {
                // 返回login
                path: '/login'
            }
        }
    } else {
        // 调用store里的方法发起异步请求
        // 如果menuList长度是0，发起请求
        if (permissStore.menuList.length === 0) {
            await permissStore.fetch_menulist()
            return {
                ...to
                // path:to.path
            }
        }
    }
})
router.afterEach((to, from) => {
    // 关闭进度条
    NProgress.done()
    let breadCrumbStore = useBreadCrumb()
    breadCrumbStore.$patch(state => {
        state.dates = to.matched
    })
})